#ifndef UART_HPP
#define UART_HPP
#include <stdint.h>
#include <stdbool.h>
#include "serialport.hpp"
#include "sdk_config.h"
#include "hc32_ddl.h"
#include "semaphore.hpp"
///> 串口设备子类
namespace device
{
	class uart:public serialport
	{
		public:
			typedef enum
			{
				UART_PARITY_TYPE_NONE = 0,	///<无校验
				UART_PARITY_TYPE_EVEN,			///<偶校验
				UART_PARITY_TYPE_ODD,				///<奇校验
			}UartParityTPDF;							///<校验位
			
			typedef enum
			{
#if USART1_ENABLED == 1
				UART_INSTANCE_1 = 0,
#endif
#if USART2_ENABLED == 1
				UART_INSTANCE_2 = 1,
#endif
#if USART3_ENABLED == 1
				UART_INSTANCE_3 = 2,
#endif
#if USART4_ENABLED == 1
				UART_INSTANCE_4 = 3,
#endif
			}UartInstanceTPDF;
			
		private:
			static bool UrtTimerCallBack(void);   ///> 串口定时回调函数，判断是否一帧接收完毕
			static void UartDmaSendCallBack1(void);
			static void UartDmaSendCallBack2(void);
			static void UartDmaSendCallBack3(void);
			static void UartDmaSendCallBack4(void);
		protected:
			uint8_t *mRecBuff;
			uint8_t *mSendBuff;
			uint16_t mBuffLen;
			uint32_t mRecLen;
			uint32_t mDmaSendHandle;
			uint32_t mDmaRecHandle;
			M4_USART_TypeDef *mSource;
			UartInstanceTPDF mUartInstance;
			systemos::semaphore *mSemaphore;
			virtual void DataSent(void);
		
		public:
			UartParityTPDF Parity;	///<校验位
			uint32_t  BaudRate;			///<波特率
			/** 
			* @brief      构造函数
			* @param[in]  tInstance  端口号
			* @retval    	None
			*/
			uart(UartInstanceTPDF tInstance, uint8_t *ucSendBuff, uint8_t *ucRecBuff, uint16_t usBuffLen);

			/** 
			* @brief      析构函数
			* @retval    	None
			*/
			virtual ~uart(void);
			
			virtual bool Open(void);
			virtual void Close(void);
			virtual bool Write(uint8_t *ucData, uint16_t usDataLen);
			virtual bool Write(uint8_t *ucCmd, uint16_t usCmdLen, uint8_t *ucData, uint16_t usDataLen);
			virtual uint16_t Read(uint8_t *ucData);
			virtual bool Read(uint8_t *ucCmd, uint16_t usCmdLen, uint8_t *ucData, uint16_t usDataLen);
			virtual void ParamSet(UartParityTPDF tParity, uint32_t uiBaudRate);
	};
}

#endif
